<?php

namespace App\Store\Models\Financial\Statements;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;

class Subject extends Model
{
    protected $table = 'site_financial_statements_subject';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::query()
            ->where(function ($query) use ($cp_id) {
                $query->where('cp_id', $cp_id)
                    ->orWhere('cp_id', 0);
            })
            ->where('is_del',1);

        // $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        // if($store_ids){//权限控制
        //     if(is_array($store_ids)){//获取当前权限下的门店
        //         $query->whereIn('store_id',$store_ids);
        //     }
        //     if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
        //         $query->where('store_id', intval($store_ids));
        //     }
        // }

        // //目标门店
        // $store_id = $request['store_id'] ?? 0;
        // if ($store_id != 0) {
        //     $query->where('store_id', $store_id);
        // }

        //订单状态
        $subject_name = $request['subject_name'] ?? '';
        if ($subject_name != '') {
            $query->where('subject_name', 'like', '%' . $subject_name . '%');
        }

        $query->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    public function getSubjectNumbers($subjectList,$codes)
    {

        $serialArr=[];
        foreach ($subjectList as $item){
            if(!empty($item['upper_number'])&&in_array($item['upper_number'],$codes)){
                $serialArr[] = $item['serial_number'];
            }
        }

        return array_values(array_merge($serialArr,$codes));
    }

}

